import { objectToAssociativeArray } from "@/modules/util"

async function getSupplyUnits(kid) {
  let res = await axios.get("/api/trade/common/getFishKindSub", {
    params: {
      kind_id: kid
    }
  })
  res = res.data.resp_data
  res.unit_size = sequenceUnitSize(res.unit_size)
  res.regular_size.push(notLimitRegularUnit)
  res.stock_types = res.unit_price.slice()
  res.stock_types = sequenceUnitPrice(res.stock_types)
  addUnitPricePrefix(res.unit_price)
  res.unit_price.push(notLimitPrice)
  res.unit_price = sequenceUnitPrice(res.unit_price)
  return res
}

//常用单位标签
function filterUnits(value) {
  if (value.unit_size_id !== 0)
    return `${value.min} - ${value.max} ${value.unit_size_name}`
  return value.unit_size_name
}

//为价格单位加上“元/”的前缀
function addUnitPricePrefix(unit_price) {
  for (let k in unit_price) {
    unit_price[k].name = `元/${unit_price[k].name}`
    unit_price[k].unit = unit_price[k].name
  }
  return unit_price
}

//转化上市时间
function parseMarketTime(value) {
  if (!value.year || !value.month || !value.day) {
    return ""
  }
  let xun
  switch (value.day) {
    case 0:
      xun = "上旬"
      break
    case 1:
      xun = "中旬"
      break
    default:
      xun = "下旬"
      break
  }
  return `${value["year"]}年${value["month"]}月${xun}`
}

const notLimitUnit = {
  name: "规格不限",
  id: 0 //规格不限此值为0
}

const notLimitRegularUnit = {
  unit_size_name: "规格不限",
  unit_size_id: 0 //规格不限此值为0
}

const notLimitPrice = {
  name: "价格面议",
  id: 0
}

//适配popup-radio数据类型
function sequenceUnitSize(unitSize) {
  unitSize.push(notLimitUnit)
  unitSize = unitSize.map(v => {
    return {
      key: v.id,
      value: v.name
    }
  })
  return unitSize
}

//适配popup-radio数据类型
function sequenceUnitPrice(price) {
  return price.map(e => {
    return {
      key: e.id,
      value: e.name
    }
  })
}

//获取表单数据
function getSupplyFormData() {
  const store = this.$store.state.posts.supply
  const formData = {
    kind_id: this.kind.id,
    title: this.title,
    size_unit_id: this.size_unit_id ? this.size_unit_id : -1,
    size_min: this.size_min,
    size_max: this.size_max,
    sale_type: this.sale_type.key,
    sale_pre: store.sale_pre,
    // sale_pre: {day:'中旬', year: 2018, month: 3},
    price_type: this.price_type,
    price: this.price,
    price_unit_id: this.price_unit_id,
    stock: this.stock,
    province: store.province.id,
    city: store.city.id,
    area: store.area.id,
    town: store.town.id,
    address: this.address,
    valided: this.valided,
    desc: this.desc,
    desc_tag: this.desc_tag,
    img: this.imgs,
    contact_name: this.contact_name,
    contact_mobile: this.contact_mobile
  }

  //如果price_type为2，则使用stock_type的值
  if (formData.price_type === 2) {
    formData.price_unit_id = this.stock_unit_id
  }

  return formData
}

//重置已填写的表单数据 0：全部清空 1：不清空kind 2：清空除开价格、规格和kind后的其他字段
function resetFormData(type = 1) {
  // console.log(type)
  this.title = ""
  this.size_unit_id = ""
  this.size_min = ""
  this.size_max = ""
  this.sale_type = { key: 1, value: "现货", comment: "现在有货" }
  this.price_type = 1
  this.price = ""
  this.price_unit_id = ""
  this.stock_unit_id = ""
  this.stock = ""
  this.valided = ""

  //vuex数据
  this.$store.commit("resetFormData", type)

  if (type === 0 || type === 1) {
    this.address = ""
    this.desc = ""
    this.desc_tag = ""
    this.contact_name = ""
    this.contact_mobile = ""
    this.accept = false
  }

  //清空供应基础时间，需要重新获取
  this.validateTime = ""
  //清空图片
  this.$refs.uploader.reset()
}

function setFormData(data) {
  this.title = data.title
  this.size_unit_id = data.size_unit_id === -1 ? 0 : data.size_unit_id
  this.size_min = data.size_min
  this.size_max = data.size_max
  this.sale_type =
    data.sale_type === 1
      ? { key: 1, value: "现货", comment: "现在有货" }
      : { key: 2, value: "预售", comment: "即将上市" }
  this.price_type = data.price_type
  this.price = data.price
  this.price_unit_id = data.price_unit_id
  this.stock_unit_id = ""
  this.stock = data.stock
  this.address = data.address
  this.valided = data.valided
  this.desc = data.desc
  this.desc_tag = data.desc_tag
  this.contact_name = data.contact_name
  this.contact_mobile = data.contact_mobile
  this.accept = false

  if (this.price_type === 2) {    
    this.stock_unit_id = this.price_unit_id
    this.price_unit_id = 0
  }

  this.$store.commit("setFormData", data)

  this.$refs.uploader.reset(data.img, data.img_view)
}

//获取表单数据
function getDemandFormData() {
  const store = this.$store.state.posts.demand
  const formData = {
    kind_id: this.kind.id,
    size_unit_id: this.size_unit_id ? this.size_unit_id : -1,
    size_min: this.size_min,
    size_max: this.size_max,
    price_type: this.price_type,
    price: this.price,
    price_unit_id: this.price_unit_id,
    stock: this.stock,
    province: store.province.id,
    city: store.city.id,
    area: store.area.id,
    town: store.town.id,
    address: this.address,
    valided: this.valided,
    desc: this.desc,
    desc_tag: this.desc_tag,
    logistics: this.logistics ? 1 : 0 ,
    contact_name: this.contact_name,
    contact_mobile: this.contact_mobile
  }

  //如果price_type为2，则使用stock_type的值
  if (formData.price_type === 2) {
    formData.price_unit_id = this.stock_unit_id
  }

  return formData
}

function resetDemandFormData(type = 1) {
  // console.log(type)
  this.size_unit_id = ""
  this.size_min = ""
  this.size_max = ""
  this.sale_type = { key: 1, value: "现货", comment: "现在有货" }
  this.price_type = 1
  this.price = ""
  this.price_unit_id = ""
  this.stock_unit_id = ""
  this.stock = ""
  this.valided = ""

  //vuex数据
  this.$store.commit("resetDemandFormData", type)

  if (type === 0 || type === 1) {
    this.address = ""
    this.desc = ""
    this.desc_tag = ""
    this.contact_name = ""
    this.contact_mobile = ""
    ;(this.accept = false), (this.logistics = false)
  }
  
  //清空供应基础时间，需要重新获取
  this.validateTime = ""

}

function setDemandFormData(data) {
  this.size_unit_id = data.size_unit_id === -1 ? 0 : data.size_unit_id
  this.size_min = data.size_min
  this.size_max = data.size_max
  this.price_type = data.price_type
  this.price = data.price
  this.price_unit_id = data.price_unit_id
  this.stock_unit_id = ""
  this.stock = data.stock
  this.address = data.address
  // this.valided = data.valided
  this.desc = data.desc
  this.desc_tag = data.desc_tag
  this.contact_name = data.contact_name
  this.contact_mobile = data.contact_mobile
  this.accept = false
  this.logistics = data.logistics == 0 ? false : true

  if (this.price_type === 2) {
    this.stock_unit_id = this.price_unit_id
    this.price_unit_id = 0
  }

  this.$store.commit("setDemandFormData", data)
}

export {
  getSupplyUnits,
  noUnit,
  filterUnits,
  parseMarketTime,
  getSupplyFormData,
  resetFormData,
  setFormData,
  resetDemandFormData,
  setDemandFormData,
  getDemandFormData
}
